<?php

class Application_Model_DbTable_Client extends Zend_Db_Table_Abstract
{

    protected $_name = 'cliente';

    public function getClient($id)
    {
        $id = (int)$id;
        $row = $this->fetchRow('cedula = ' . $id);
        if (!$row) {
            throw new Exception("Could not find row $id");
        }
        return $row->toArray();
    }
    
    public function addClient($cedula, $nombre,$fecha,$telefono,$correo)
    {
       
        $date_formatted = date('Y-m-d', strtotime($fecha)).' 00:00:00';
        $data = array(
            'cedula' => $cedula,
            'nombre' => $nombre,
            'fecha' => $date_formatted,
            'telefono' => $telefono,
            'correo' => $correo,
        );
        $this->insert($data);
    }
    
    public function updateClient($id,$cedula, $nombre,$fecha,$telefono,$correo)
    {
        $date_formatted = date('Y-m-d', strtotime($fecha)).' 00:00:00';
        $data = array(
            'nombre' => $nombre,
            'fecha' => $date_formatted,
            'telefono' => $telefono,
            'correo' => $correo,
        );
       $this->update($data, 'cedula = ' . (int)$id);
         // $this->update($data, 'cedula = ' . (int)"114920627");
    }
    
    public function deleteClient($id)
    {
        $this->delete('cedula = ' . (int)$id);
    }

    public function find(){
        $row = $this->fetchall($this->select()
                ->limit(10, 0)
                ->order('fecha_insercion desc'));
        if (!$row) {
            throw new Exception("Could not find row");
        }
        return $row->toArray();
    }

    public function findClient($id, $name)
    {
        if(!empty($id) && !empty($name)){
            $row = $this->fetchall($this->select()
              ->where('nombre = ?', $name)
              ->where('cedula = ?', $id)
            );
        } else {
            if(!empty($id)){
                $row = $this->fetchall($this->select()
                  ->where('cedula = ?', $id)
                );
            } else {
                $row = $this->fetchall($this->select()
                  ->where('nombre = ?', $name)
                );
            }
        }
        
        if (!$row) {
            throw new Exception("Could not find row $id");
        }
        return $row->toArray();
    }


}

